/**
 * @author hAmmeRoL, created: 2008-7-9
 * SJTU
 */

package protocol.generic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;

import protocol.domain.GroupEntry;

/**
 * Class representing a group in the buddy list
 * 
 * @author hAmmeRoL
 * 
 */
public class Group implements Serializable {

	private static final long serialVersionUID = 1L;

	// group name, default to unfiled group
	private String name;

	// contains all the group entries
	private ArrayList<GroupEntry> groupEntries;

	/**
	 * Construct a Group object using the group name
	 * 
	 * @param name
	 *            the group name to be used
	 */
	public Group(String name) {
		this.name = name;
		groupEntries = new ArrayList<GroupEntry>();
	}

	/**
	 * Equivalent to Group("Unfiled Group");
	 */
	public Group() {
		this.name = "Unfiled Group";
		groupEntries = new ArrayList<GroupEntry>();
	}

	/**
	 * Check whether this group contains a specific GroupEntry
	 * 
	 * @precondition. none
	 * @postcondition. none
	 * @modifies. none
	 * 
	 * @param entry
	 *            the GroupEntry to be checked
	 * @return true if this group contains this entry, otherwise false
	 */
	public boolean containsEntry(GroupEntry entry) {
		if (groupEntries.contains(entry))
			return true;
		return false;
	}

	/**
	 * Check whether this group contains a specific GroupEntry with the id
	 * provided
	 * 
	 * @precondition. none
	 * @postcondition. none
	 * @modifies. none
	 * 
	 * @param id
	 *            the id of the GroupEntry to be checked
	 * @return the entry with the id or null if nothing is found
	 */
	public GroupEntry getGroupEntryByID(String id) {
		for (GroupEntry entry : groupEntries) {
			if (entry.getID().equalsIgnoreCase(id)) {
				return entry;
			}
		}
		return null;
	}

	/**
	 * Update the group sequence according to the comparator. Currently, online
	 * users get highest priority, followed by the natural order of the alias of
	 * this entry
	 * 
	 * @precondition. none
	 * @postcondition. the group is sorted according to the comparator
	 * @modifies. the sequence of the group entries
	 * 
	 */
	public void update() {
		Collections.sort(groupEntries, new GroupEntryComparator());
	}

	/**
	 * Get the current number of entries in the group
	 * 
	 * @precondition. none
	 * @postcondition. none
	 * @modifies. none
	 * 
	 * @return the number of entries in this group
	 */
	public int getNum() {
		return groupEntries.size();
	}

	/**
	 * Get the group name
	 * 
	 * @precondition. none
	 * @postcondition. none
	 * @modifies. none
	 * 
	 * @return the group name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Set the group name to id
	 * 
	 * @precondition. none
	 * @postcondition. the name is changed to the new one
	 * @modifies. group name
	 * 
	 * @param name
	 *            the new group name to be set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Add one group entry to this group
	 * 
	 * @precondition. none
	 * @postcondition. the new group entry is added
	 * @modifies. the group entry list
	 * 
	 * @param entry
	 *            the group entry to add into
	 */
	public void addGroupEntry(GroupEntry entry) {
		groupEntries.add(entry);
		Collections.sort(groupEntries, new GroupEntryComparator());
	}

	/**
	 * Add all the entries into this group
	 * 
	 * @precondition. none
	 * @postcondition. the new group is combined into this one
	 * @modifies. this group list
	 * 
	 * @param newGroup
	 *            the new group to be combined
	 */
	public void combineGroup(Group newGroup) {
		ArrayList<GroupEntry> entries = newGroup.getGroupEntries();
		for (GroupEntry entry : entries) {
			addGroupEntry(entry);
		}
	}

	/**
	 * Get the group entry at the index position
	 * 
	 * @precondition. none
	 * @postcondition. none
	 * @modifies. none
	 * 
	 * @param index
	 *            the position of the group entry to get
	 * @return the group entry at the index position
	 */
	public GroupEntry getGroupEntryByIndex(int index) {
		return groupEntries.get(index);
	}
	
	
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public void removeGroupEntryByID(String id){
		for (int i=0;i<groupEntries.size();i++) {
			GroupEntry entry=groupEntries.get(i);
			if (entry.getID().equalsIgnoreCase(id)) {
				groupEntries.remove(i);
			}
		}
	}
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	/**
	 * Get the group entries
	 * 
	 * @precondition. none
	 * @postcondition. none
	 * @modifies. none
	 * 
	 * @return the group entries in this group
	 */
	public ArrayList<GroupEntry> getGroupEntries() {
		return groupEntries;
	}

	/**
	 * Two groups are equal if their names are the same.
	 */
	@Override
	public boolean equals(Object g) {
		if (g instanceof Group) {
			Group temp = ((Group) g);
			if (temp.name.equals(name)) {
				return true;
			}
			return false;
		} else {
			return false;
		}
	}

}
